package main

import (
	"bufio"
	"fmt"
	//"fmt"
	"log"
	"net"
	"os"
	. "tcpDemo/chatroom/chat"
)

func main() {

	fmt.Println("请输入服务器地址和端口，格式 IP:Port")
	in := bufio.NewReader(os.Stdin)
	line, _, _ := in.ReadLine()

	fmt.Println("connect to ", string(line))
	conn, err := net.Dial("tcp", string(line))

	if err != nil {
		log.Fatal(err)
	}

	defer conn.Close()

	out := bufio.NewWriter(os.Stdout)

	client := CreateClient(conn)
	client.SetConnectStr(string(line))
	client.PutOutgoing("connect")

	go func() {
		for {
			out.WriteString(client.GetIncoming() + "\n")
			out.Flush()
		}
	}()

	for {
		line, _, _ := in.ReadLine()
		if string(line) == ":quit" || string(line) == ":q" {
			client.PutOutgoing(string(line))
			os.Exit(0)
		}

		client.PutOutgoing(string(line))

	}

}
